package com.kunda.netty.protobuf;

import java.util.Arrays;

import com.google.protobuf.InvalidProtocolBufferException;

public class TestSubscribeReqProto {
	private static byte[] encode(SubscribeReqProto.SubscribeReq req){
		return req.toByteArray();
	}
	
	private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
		return SubscribeReqProto.SubscribeReq.parseFrom(body);
	}
	
	private static SubscribeReqProto.SubscribeReq createSubscribeReq(){
		SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
		builder.setSubReqId(1)
		       .setUserName("helloworld")
		       .setProductName("Netty")
		       .addAllAddress(Arrays.asList(new String[]{"suzhou","nanjing"}));
		return builder.build();
	}
	
	public static void main(String[] args) throws Exception {
		SubscribeReqProto.SubscribeReq req = createSubscribeReq();
		System.out.println("before encode:"+req);
		
		SubscribeReqProto.SubscribeReq req2 = decode(encode(req));
		System.out.println("after decode:"+req2);
		
		System.out.println(req2.equals(req));
	}
}
